<template>
  <div class="addjob-container">
    <el-form ref="formRef" :model="data.formData" label-width="80px">
      <el-form-item
        label="职位名称"
        prop="name"
        :rules="[{ required: true, message: '请输入职位名称' }]"
      >
        <el-input
          v-model="data.formData.name"
          placeholder="请输入职位名称"
          maxlength="100"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="职位描述" prop="description">
        <el-input
          v-model="data.formData.description"
          type="textarea"
          maxlength="500"
          autocomplete="off"
          :rows="5"
          placeholder="请输入职位描述"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="部门"
        prop="departmentId"
        :rules="[{ required: true, message: '请选择部门' }]"
      >
        <el-tree-select
          v-model="data.formData.departmentId"
          :data="data.departmentTrees"
          default-expand-all
          :props="{ label: 'name', value: 'id' }"
          check-strictly
          :render-after-expand="false"
          style="width: 240px"
        />
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <el-popconfirm
        confirm-button-text="是"
        cancel-button-text="否"
        title="是否要重置表单?"
        @confirm="events.handleReset"
      >
        <template #reference>
          <el-button type="info" plain>重置</el-button>
        </template>
      </el-popconfirm>
      <el-button type="primary" @click="events.handleSubmit">提交</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { FormInstance } from 'element-plus';
import { onMounted, reactive, ref } from 'vue';
import { getDeprtmentTree } from '../../../functions/department';
import { addJob } from '../../../functions/job';
import { toast } from '../../../utils/tools';
import { useRouter } from 'vue-router';

const data = reactive({
  formData: {
    name: '',
    description: '',
    departmentId: '',
  },
  departmentTrees: [],
});

const formRef = ref(null);
const router = useRouter();

const events = {
  handleReset() {
    const form = formRef.value as FormInstance | null;
    if (form) {
      form.resetFields();
      data.formData = {
        name: '',
        description: '',
        departmentId: '',
      };
    }
  },
  handleSubmit() {
    const form = formRef.value as FormInstance | null;
    if (form) {
      form
        .validate()
        .then(async (valid) => {
          if (valid) {
            // 提交表单
            const res = await addJob(data.formData);
            if (res.code >= 0) {
              router.back();
              toast.success(res.message || '添加成功');
            } else {
              toast.error(res.message || '添加失败');
            }
          }
        })
        .catch((e) => {});
    }
  },
};

onMounted(async () => {
  // 获取部门树
  const res = await getDeprtmentTree();
  if (res.code >= 0) {
    data.departmentTrees = res.data;
  }
});
</script>

<style scoped lang="scss">
.addjob-container {
  padding: 20px;
  padding-bottom: 20%;
}
.btn-container {
  margin-top: 20px;
  text-align: right;
}
</style>
